package com.lingshi.shopping_cart_customer_api.controller;

import com.lingshi.shopping.entity.CartGoods;
import com.lingshi.shopping.result.BaseResult;
import com.lingshi.shopping.service.CartService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/cart")
public class ShoppingCartController {

    @DubboReference
    private CartService cartService;

    /**
     * 查询当前用户购物车
     * @param userId
     * @return
     */
    @GetMapping("/findCartList")
    public BaseResult findCartList(@RequestHeader("userId") Long userId) {
        return BaseResult.success(cartService.findCartList(userId));
    }

    /**
     * 添加商品到购物车
     * @param userId 用户id
     * @param cartGoods 购物车商品
     * @return
     */
    @PostMapping("/addCart")
    public BaseResult addCart(@RequestHeader("userId") Long userId, @RequestBody CartGoods cartGoods) {
        cartService.addCart(userId, cartGoods);
        return BaseResult.success();
    }

    /**
     * 修改购物车商品属性
     * @param userId 用户id
     * @param goodId 商品id
     * @param num 购买数量
     * @return
     */
    @GetMapping("/handleCart")
    public BaseResult handleCart(@RequestHeader("userId") Long userId,Long goodId,Integer num) {
        cartService.handleCart(userId, goodId, num);
        return BaseResult.success();
    }

    /**
     * 删除购物车商品
     * @param userId 用户id
     * @param goodId 商品id
     * @return
     */
    @DeleteMapping("/deleteCart")
    public BaseResult deleteCart(@RequestHeader("userId") Long userId,Long goodId) {
        cartService.deleteCartOption(userId,goodId);
        return BaseResult.success();
    }
}
